package com.taichuan.keysupport.panel;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.taichuan.keysupport.Key;
import com.taichuan.keysupport.R;
import com.taichuan.keysupport.keyset.KeySet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by OISNY on 2016/8/19.
 */
public abstract class PanelManager implements KeySet.OnKeySupportListener, Runnable {
    protected Context mContext;
    private List<String> mLists = new ArrayList<String>();
    protected KeySet mKeySet;
    private PanelAdapter mPanelAdapter;
    private ImageView mImageViewX;
    private ImageView mImageViewJ;
    private TextView mTextViewZero;
    private GridView mGridView;
    protected PopupWindow mPopupWindow;
    protected EditText mEditText;

    int keyIndex = 0;
    int oldKeyIndex = -1;
    boolean isSameKeyIndex = false;
    int jIndex;

    public abstract int getPanelLayoutResource();

    public abstract int getPanelItemResource();

    public abstract KeySet initKeySet();

    public PanelManager(Context context) {
        mContext = context;
        mKeySet = initKeySet();
    }

    public void showPopupWindow(EditText editText) {
        this.mEditText = editText;
        mEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mEditText.getEditableText().length() == 0) {
                    oldKeyIndex = -1;
                }
            }
        });
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            dissmissPopupWindow();
        }
        if (mPopupWindow == null) {
            View view = LayoutInflater.from(mContext).inflate(getPanelLayoutResource(),
                    null);
            mGridView = (GridView) view.findViewById(R.id.key_gridview);
            mImageViewX = (ImageView) view.findViewById(R.id.key_img_x);
            mImageViewJ = (ImageView) view.findViewById(R.id.key_img_j);
            mTextViewZero = (TextView) view.findViewById(R.id.key_text_zero);
            mKeySet.updatePanelView();
            mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, false);
        }
        showPopupWindowInView(editText);
    }

    protected void showPopupWindowInView(EditText editText) {
        mPopupWindow.showAsDropDown(editText);
    }


    public void dissmissPopupWindow() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
    }

    public void close() {
        dissmissPopupWindow();
        if (mPopupWindow != null) {
            mPopupWindow = null;
        }
        if (mGridView != null) {
            mGridView = null;
        }
        if (mKeySet != null) {
            mKeySet = null;
        }
    }

    @Override
    public void setXDrawable(int drawableId, int xIndex) {
        mImageViewX.setImageResource(drawableId);
    }

    @Override
    public void setXDrawable(int drawableId, boolean xClick) {
        mImageViewX.setImageResource(drawableId);
    }

    @Override
    public void setZeroText(String text, int jIndex) {
        mTextViewZero.setText(text);
    }

    @Override
    public void setJDrawable(int drawableId, int jIndex) {
        mImageViewJ.setImageResource(drawableId);
        this.jIndex = jIndex;
    }

    @Override
    public void upGridViewData(List<String> list) {
        mLists.clear();
        mLists.addAll(list);
        if (mPanelAdapter == null) {
            mPanelAdapter = new PanelAdapter(mContext);
            mGridView.setAdapter(mPanelAdapter);
        } else {
            mPanelAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void updateEdit(String text) {
        setEditText(text);
    }

    public class PanelAdapter extends BaseAdapter {
        public Context context;

        public PanelAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return mLists.size();
        }

        @Override
        public Object getItem(int position) {
            return mLists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(
                        getPanelItemResource(), null);
                viewHolder.tv = (TextView) convertView
                        .findViewById(R.id.tv_key);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tv.setText(mLists.get(position));
            return convertView;
        }

        class ViewHolder {
            TextView tv;
        }

    }

    int getKeyIndex(int keyCode) {
        int keyIndex = 0;
        switch (keyCode) {
            case Key.Convert.KEYCODE_1:
                keyIndex = 1;
                break;
            case Key.Convert.KEYCODE_2:
                keyIndex = 2;
                break;
            case Key.Convert.KEYCODE_3:
                keyIndex = 3;
                break;
            case Key.Convert.KEYCODE_4:
                keyIndex = 4;
                break;
            case Key.Convert.KEYCODE_5:
                keyIndex = 5;
                break;
            case Key.Convert.KEYCODE_6:
                keyIndex = 6;
                break;
            case Key.Convert.KEYCODE_7:
                keyIndex = 7;
                break;
            case Key.Convert.KEYCODE_8:
                keyIndex = 8;
                break;
            case Key.Convert.KEYCODE_9:
                keyIndex = 9;
                break;
            case Key.Convert.KEYCODE_0:
                keyIndex = 10;
        }
        return keyIndex;
    }

    public void setEditText(String text) {
        if (mEditText == null) {
            return;
        }
        Editable editable = mEditText.getEditableText();
        if (editable != null) {
            if (text.equals("-1")) {
                if (editable.length() > 0) {
                    editable.delete(editable.length() - 1, editable.length());
                }
            } else {
                if (isSameKeyIndex && isDelayInput() && editable.length() > 0) {
                    editable.delete(editable.length() - 1, editable.length());
                }
                editable.insert(editable.length(), text);
            }
        }
    }

    @Override
    public void run() {
        isSameKeyIndex = false;
        oldKeyIndex = -1;
    }

    abstract boolean isDelayInput();
}
